using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using TeamCommunitySolution.Domain.DataLayer.Leagues;
using TeamCommunitySolution.Domain.DataLayer.Base;
using TeamCommunitySolution.Modules.Leagues;
using TeamCommunitySolution.Modules.Leagues.Requests;
using TeamCommunitySolution.Modules.Base;
using TeamCommunitySolution.Modules.Base.Requests;

namespace TeamCommunitySolution.Controllers
{
    [Authorize]
    public class LeagueController : Controller
    {
        /// <summary>
        /// Team manager instance.
        /// </summary>
        private LeagueManager _leagueManager = null;

        private BaseManager _baseManager = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="LeagueController"/> class.
        /// </summary>
        public LeagueController()
            : this(MvcApplication.LeagueManager, MvcApplication.BaseService)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamController"/> class.
        /// </summary>
        /// <param name="leagueManager">The league manager.</param>
        public LeagueController(LeagueManager leagueManager, BaseManager baseManager)
        {
            this._leagueManager = leagueManager;
            this._baseManager = baseManager;
        }

        //
        // GET: /League/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /League/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /League/Create
        public ActionResult LeagueCreate()
        {
            return View();
        }

        //
        // POST: /League/Create
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult LeagueCreate(FormCollection collection)
        {
            try
            {
                League league = new League();
                UpdateModel(league, collection.ToValueProvider());

                SystemUserRequest userRequest = new SystemUserRequest();
                userRequest.Username = User.Identity.Name;

                LeagueRequest request = new LeagueRequest();
                request.IsComplete = league.IsComplete;
                request.LeagueName = league.Name;
                request.Owner = this._baseManager.GetSystemUserByUsername(userRequest).SystemUser;
                request.TeamSize = league.TeamSize;

                this._leagueManager.CreateLeague(request);

                return View("OperationCompleted");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /League/Edit/5
        [Authorize]
        public ActionResult LeagueEdit(string id)
        {
            LeagueRequest request = new LeagueRequest();
            request.Id = id;

            League league = _leagueManager.GetLeague(request).League;
            return View("LeagueEdit", league);
        }

        //
        // POST: /League/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult LeagueEdit(string id, FormCollection collection)
        {
            try
            {
                League league = new League();
                UpdateModel(league, collection.ToValueProvider());

                SystemUserRequest userRequest = new SystemUserRequest();
                userRequest.Username = User.Identity.Name;

                LeagueRequest request = new LeagueRequest();
                request.Id = league.Id;
                request.IsComplete = league.IsComplete;
                request.LeagueName = league.Name;
                request.TeamSize = league.TeamSize;
                request.Owner = this._baseManager.GetSystemUserByUsername(userRequest).SystemUser;

                this._leagueManager.UpdateLeague(request);

                return View("OperationCompleted");
            }
            catch
            {
                return View();
            }
        }

        /// <summary>
        /// Invites the team.
        /// </summary>
        /// <param name="teamName">Name of the team.</param>
        /// <param name="message">The message.</param>
        /// <param name="leagueId">The league id.</param>
        /// <returns>Action result.</returns>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
        [Authorize]
        public ActionResult InviteTeam(string teamName, string message, string leagueId)
        {
            try
            {
                /*
                Team team = _leagueManager.TeamGetByName(teamName);
                League league = _leagueManager.LeagueGet(leagueId);

                LeagueRequest request = new LeagueRequest();

                

                this._leagueManager.LeagueInviteTeamTo(team, league, message, MvcApplication.HostName);
                */
                return View("OperationCompleted");
            }
            catch
            {
                return View();
            }
        }

        /// <summary>
        /// Accepts the invite.
        /// </summary>
        /// <param name="leagueid">The leagueid.</param>
        /// <param name="teamid">The teamid.</param>
        /// <returns>Action result.</returns>
        [AcceptVerbs(HttpVerbs.Get)]
        [Authorize]
        public ActionResult AcceptInvite(string leagueid, string teamid)
        {
            try
            {
                /*
                League league = this._leagueManager.LeagueGet(leagueid);
                this._leagueManager.LeagueAcceptInvite(_leagueManager.UserGetByUsername(User.Identity.Name), leagueid, teamid);
                return View("OperationCompleted");
                 * */
            }
            catch (Exception ex)
            {
                return View("OperationCompleted");
            }
        }
    }
}
